/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolz;

/**
 *
 * @author Bunkowski
 */
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

public class JDirectoryScanner {

    private static File root;
    private static ArrayList<File> fileList;
    protected static boolean stop = false;
    private static FileFilter filter = new FileFilter() {

        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }

            String name = file.getName();
            if (name.endsWith(".csv")) {
                return true;
            }
            if (name.endsWith(".pl")) {
                return true;
            }
            if (name.endsWith(".xls")) {
                return true;
            }
            return false;
        }
    };

    public static ArrayList<File> scan(File rootfile) {
        fileList = new ArrayList<File>();
        if (rootfile.isDirectory()) {
            root = rootfile;
            treeWalk(root);
        }
        else{
            fileList.add(rootfile);
        }
        return (fileList);
    }

    //frei nach der dclj FAQ (www.dclj.de)
    public static void treeWalk(File root) {

        File[] files = root.listFiles(filter);
        if (files == null || files.length < 1) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                treeWalk(files[i]);
            } else {
                System.out.println(files[i].getName());
                fileList.add(files[i]);
            }
        }
    }
}